package com.inspur.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.pojo.user;
import com.inspur.service.userService;
@Controller
public class usercontro {
	@Autowired
	private userService userService;
	
	@RequestMapping("/userInfoList")
	public String userInfoList(Model model,HttpServletRequest request,@RequestParam(value="pn",defaultValue="1") Integer pn){
		String  id = request.getParameter("id");
		String  username = request.getParameter("username");
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		map.put("username", username);
		PageHelper.startPage(pn, 3);
		List<user> userList = userService.getUserList(map);
		PageInfo<user> pageInfo = new PageInfo<user>(userList,userList.size());
		
		model.addAttribute("pageInfo",pageInfo );
		model.addAttribute("id", id);
		model.addAttribute("username", username);
		
		return "/user/userInfo.jsp";
	}
	
	/**
	 * 用户的删除
	 * @param userIds
	 * @return
	 */
	@RequestMapping("/deleteUser")
	public String deleteUsers(String ids){
		String id[] = ids.split(",");
		int user_ids[] = new int[id.length];
		for(int i=0;i<id.length;i++){
			user_ids[i] = Integer.parseInt(id[i]);
		}
		userService.deleteUser(user_ids);
		
		//删除数据后要重新查询一次
		return "redirect:userInfoList";
		
	}
	/**
	 * 用户状态启用
	 * @param userIds
	 * @return
	 */
	@RequestMapping("/changeTF")
	public String changeTF(String ids){
		String id[] = ids.split(",");
		int user_ids[] = new int[id.length];
		for(int i=0;i<id.length;i++){
			user_ids[i] = Integer.parseInt(id[i]);
		}
		userService.changeTF(user_ids);
		
		//重新查询一次
		return "redirect:userInfoList";
		
	}
	/**
	 * 用户状态禁用
	 * @param userIds
	 * @return
	 */
	@RequestMapping("/userClose")
	public String userClose(String ids){
		String id[] = ids.split(",");
		int user_ids[] = new int[id.length];
		for(int i=0;i<id.length;i++){
			user_ids[i] = Integer.parseInt(id[i]);
		}
		userService.userClose(user_ids);
		
		//重新查询一次
		return "redirect:userInfoList";
	}
	/**
	 * 用户密码恢复初始
	 * @param userIds
	 * @return
	 */
	@RequestMapping("/resetPass")
	public String resetPass(String ids){
		String id[] = ids.split(",");
		int user_ids[] = new int[id.length];
		for(int i=0;i<id.length;i++){
			user_ids[i] = Integer.parseInt(id[i]);
		}
		userService.resetPass(user_ids);
		
		//要重新查询一次
		return "redirect:userInfoList";
	}
	/**
	 * 用户的添加保存
	 * @return
	 */
	@RequestMapping("/useInfoAdd")
	public String userAdd(user user,Model model){
		//1.主键如何生成--int 1)oracle数据库-采用序列生成--目前程序中采用序列生成 2)自定义拼装int型的主键-不允许重复  2.VARCHAR2(64) UUID
	
		//处理用户信息中页面未维护的要素
		//1.获取当前时间
		Date today = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String time =format.format(today);
		user.setRegisterTime(time);
		user.setLastTime(time);
		user.setIsEnable("T");
		user.setAnswer(user.getPassword());
		
		userService.addUser(user);
		
		model.addAttribute("success", "保存成功");
		model.addAttribute("member",user);
		model.addAttribute("flag", "1");
		
		return "/user/userInfoAdd.jsp";
		
	}
	
	@RequestMapping("/checkUser")
	@ResponseBody
	public Map checkUser(String username) {
		System.out.println("前台Ajax传递过来的参数: " +username);
		List<user> selectUser = userService.getUserByUserName(username);
		Map map = new HashMap();
		map.put("selectUser", selectUser);
		if (selectUser.size() >= 1) {
			map.put("msg", "已存在该用户");
		}
		System.out.println("map==="+map);
		return map;
	}
	
	@RequestMapping("/modifyUser")
	public String modifyUser(String id,Model model){
	  int user_id = Integer.parseInt(id);
	  user user = userService.getUserById(user_id);
	  model.addAttribute("member", user);
	  return "/user/userInfoUpdate.jsp";
	}
	
	/**
	 * 用户的修改更新
	 * @return
	 */
	@RequestMapping("/userInfoUpdate")
	public String userupdate(user user,Model model){
		//1.获取当前时间
		Date today = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String time =format.format(today);
		user.setLastTime(time);
		
		userService.updateUser(user);
		
		model.addAttribute("success", "修改成功");
		model.addAttribute("member",user);
		model.addAttribute("flag", "1");
		
		return "/user/userInfoUpdate.jsp";
		
	}
	
	
	/*
	 * 
	@RequestMapping("getuserById")
	public int getCustById(int id, Model model){
		// 调用业务层进行客户信息的获取
		user user = userService.getuserById(id);
		model.addAttribute("user", user);
		return 1;
	}
	@RequestMapping("getuserByname")
	public String getCustByname(String username, Model model){
		// 调用业务层进行客户信息的获取
		user user = userService.getuserByname(username);
		model.addAttribute("user", user);
		return "user";
	}
	*/
}
